package com.webreport.dao.hibernate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webreport.dao.SessionDao;
import com.webreport.exception.DaoException;
import com.webreport.model.QueryModel;
import com.webreport.model.db.SessionMonitor;
import com.webreport.utils.DaoUtils;
@Repository
public class SessionDaoHibernate extends GenericDaoHibernate<SessionMonitor,String> implements SessionDao{

	public SessionDaoHibernate() {
		super(SessionMonitor.class);
	}
	public SessionMonitor queryById(String clientid) throws DaoException{
		QueryModel query = new QueryModel(SessionMonitor.class.getName(),"clientid =:clientid and enabled=:enabled");
		query.addParam("clientid", clientid);
		query.addParam("enabled", true);
		List<SessionMonitor> results = DaoUtils.queryHQL(getHibernateTemplate(), query);
		if (results != null && results.size() >0){
			return results.get(0);
		}else{
			return null;
		}
	}
	public List<SessionMonitor> queryByUsername(String username)throws DaoException{
		QueryModel query = new QueryModel(SessionMonitor.class.getName(),"username =:username and enabled=:enabled");
		query.addParam("username", username);
		query.addParam("enabled", true);
		List<SessionMonitor> results = DaoUtils.queryHQL(getHibernateTemplate(), query);
		return results;
	}
	
	public void disableSession(String clientid) throws DaoException{
		StringBuffer sql = new StringBuffer();
		sql.append("update ").append(SessionMonitor.class.getName()).append(" set enabled = :enabled where clientid = :clientid");
		Map params = new HashMap();
		params.put("enabled", false);
		params.put("clientid",clientid);
		DaoUtils.update(getHibernateTemplate(), sql.toString(), params);
	}
}
